<?php

//generate a random 10 characters code
$shoppingcart_id = generateid();

//get the current date
$today = date("Y-m-d");

//get the users's ip
$user_ip = $_SERVER['REMOTE_ADDR'];

//now, you can register the shopping cart into the DB
mysql_query("INSERT INTO carts (ID, DATE, IP) VALUES ('$shoppingcart_id' , '$today', '$user_ip')");

//after registering the new cart, set the cookie to the user's browser

//set the lifetime of the cookie
$expire = time()+60*60*24*30; // (1 month lifetime)

//set the domain for which the cookie is valid
$dom = $_SERVER['SERVER_NAME'];
if($dom[0] == "w")
	{
	//truncate the first three characters
	$domain = substr($dom, 3);
	}
else
	{
	//add point
	$domain = ".".$dom;
	}

//finally, send the cookie to the user agent
setcookie("shoppingcart_id", $shoppingcart_id, $expire, "/", $domain);

?>